import JsBridge from '@/utils/JsBridge.js'
import { createRouter, createWebHashHistory } from "vue-router";
import { getStore, setStore, globalLoading } from "../utils/utils";

const routes = [
  {
    path: "/",
    component: () => import("@/views/login"),
  },
  {
    path: "/forget",
    component: () => import("@/views/login/forget"),
  },
  {
    path: "/share",
    name: "分享",
    component: () => import("@/views/share"),
    meta: {
      openName: "/share",
    },
  },
  {
    path: "/home",
    component: () => import("@/views/home"),
    redirect: "/index",
    children: [
      // 工作台
      {
        path: "/index",
        name: "工作台",
        component: () => import("@/views/index"),
        meta: {
          openName: "/index",
        },
      },
      {
        path: "/index/uploadOrder",
        name: "资料上传订单",
        component: () => import("@/views/index/uploadOrder"),
        meta: {
          openName: "/uploadOrder",
        },
      },
      {
        path: "/index/uploadDetail",
        name: "资料上传-订单详情",
        component: () => import("@/views/index/uploadDetail"),
        meta: {
          openName: "/uploadDetail",
        },
      },
      {
        path: "/index/uploadData",
        name: "资料上传",
        component: () => import("@/views/index/uploadData"),
        meta: {
          openName: "/uploadData",
        },
      },
      {
        path: "/index/auditOrder",
        name: "资料审核订单",
        component: () => import("@/views/index/auditOrder"),
        meta: {
          openName: "/auditOrder",
        },
      },
      {
        path: "/index/auditDetail",
        name: "资料审核详情",
        component: () => import("@/views/index/auditDetail"),
        meta: {
          openName: "/auditDetail",
        },
      },
      {
        path: "/index/incomeList",
        name: "佣金政策",
        component: () => import("@/views/index/incomeList"),
        meta: {
          openName: "/incomeList",
        },
      },
      {
        path: "/index/phoneActive",
        name: "号卡激活",
        component: () => import("@/views/index/phoneActive"),
        meta: {
          openName: "/phoneActive",
        },
      },
      {
        path: "/index/phoneActive/detail",
        name: "号卡激活引导",
        component: () => import("@/views/index/phoneActive/detail"),
        meta: {
          openName: "/phoneActive",
        },
      },
      {
        path: "/index/phoneActive/flow",
        name: "开卡流程",
        component: () => import("@/views/index/phoneActive/flow"),
        meta: {
          openName: "/phoneActive",
        },
      },
      {
        path: "/index/phoneRecharge",
        name: "话费充值",
        component: () => import("@/views/index/phoneRecharge"),
        meta: {
          openName: "/phoneRecharge",
        },
      },
      {
        path: "/index/phoneRecharge/detail",
        name: "话费充值引导",
        component: () => import("@/views/index/phoneRecharge/detail"),
        meta: {
          openName: "/phoneRecharge",
        },
      },
      {
        path: "/index/phoneRecharge/flow",
        name: "充值流程",
        component: () => import("@/views/index/phoneRecharge/flow"),
        meta: {
          openName: "/phoneRecharge",
        },
      },

      // 店铺管理
      {
        path: "/shop",
        name: "店铺管理",
        component: () => import("@/views/shop"),
        meta: {
          openName: "/shop",
        },
      },
      {
        path: "/shop/distributor",
        name: "我的分销商",
        component: () => import("@/views/shop/distributor"),
        meta: {
          openName: "/distributor",
        },
      },
      {
        path: "/shop/supplier",
        name: "我的供应商",
        component: () => import("@/views/shop/supplier"),
        meta: {
          openName: "/supplier",
        },
      },
      {
        path: "/shop/supplierBind",
        name: "供应商-绑定",
        component: () => import("@/views/shop/supplierBind"),
        meta: {
          openName: "/supplierBind",
        },
      },
      {
        path: "/shop/customer",
        name: "客服",
        component: () => import("@/views/shop/customer"),
        meta: {
          openName: "/customer",
        },
      },
      {
        path: "/shop/register",
        name: "商户代注册",
        component: () => import("@/views/shop/register"),
        meta: {
          openName: "/register",
        },
      },
      {
        path: "/shop/registerSuccess",
        name: "商户代注册-成功",
        component: () => import("@/views/shop/registerSuccess"),
        meta: {
          openName: "/registerSuccess",
        },
      },
      {
        path: "/shop/cardQr",
        name: "二维码名片",
        component: () => import("@/views/shop/cardQr"),
        meta: {
          openName: "/cardQr",
        },
      },
      {
        path: "/shop/realName",
        name: "实名认证",
        component: () => import("@/views/shop/realName"),
        meta: {
          openName: "/realName",
        },
      },
      {
        path: "/shop/update",
        name: "修改店铺名称",
        component: () => import("@/views/shop/update"),
        meta: {
          openName: "/update",
        },
      },
      {
        path: "/shop/supplier/wechat",
        name: "供应商微信客服",
        component: () => import("@/views/shop/supplier/wechat"),
        meta: {
          openName: "/supplier",
        },
      },
      {
        path: "/shop/wechat",
        name: "微信客服",
        component: () => import("@/views/shop/wechat"),
        meta: {
          openName: "/supplier",
        },
      },

      // 资产
      {
        path: "/capital",
        name: "资产",
        component: () => import("@/views/capital"),
        meta: {
          openName: "/capital",
        },
      },
      {
        path: "/capital/accountConsume",
        name: "账户消费明细",
        component: () => import("@/views/capital/accountConsume/list"),
        meta: {
          openName: "/accountConsume",
        },
      },
      {
        path: "/capital/accountRecharge",
        name: "账户充值记录",
        component: () => import("@/views/capital/accountRecharge/list"),
        meta: {
          openName: "/accountRecharge",
        },
      },
      {
        path: "/capital/earning",
        name: "收益明细",
        component: () => import("@/views/capital/earning/list"),
        meta: {
          openName: "/earning",
        },
      },
      {
        path: "/capital/earning/detail",
        name: "收益明细-详情",
        component: () => import("@/views/capital/earning/detail"),
        meta: {
          openName: "/earning",
        },
      },
      {
        path: "/capital/earningWithdraw",
        name: "收益提现明细",
        component: () => import("@/views/capital/earningWithdraw/list"),
        meta: {
          openName: "/earningWithdraw",
        },
      },
      {
        path: "/capital/earningWithdraw/detail",
        name: "收益提现明细-详情",
        component: () => import("@/views/capital/earningWithdraw/detail"),
        meta: {
          openName: "/earningWithdraw",
        },
      },
      {
        path: "/capital/tixian",
        name: "提现",
        component: () => import("@/views/capital/tixian"),
        meta: {
          openName: "/tixian",
        },
      },
      {
        path: "/capital/benefit",
        name: "分润明细",
        component: () => import("@/views/capital/benefit/list"),
        meta: {
          openName: "/benefit",
        },
      },
      {
        path: "/capital/benefit/detail",
        name: "分润明细-详情",
        component: () => import("@/views/capital/benefit/detail"),
        meta: {
          openName: "/benefit",
        },
      },
      {
        path: "/capital/benefitWithdraw",
        name: "分润提现明细",
        component: () => import("@/views/capital/benefitWithdraw/list"),
        meta: {
          openName: "/benefitWithdraw",
        },
      },
      {
        path: "/capital/benefitWithdraw/detail",
        name: "分润提现明细-详情",
        component: () => import("@/views/capital/benefitWithdraw/detail"),
        meta: {
          openName: "/benefitWithdraw",
        },
      },

      // 更多
      {
        path: "/more",
        name: "更多",
        component: () => import("@/views/more"),
        meta: {
          openName: "/more",
        },
      },
      {
        path: "/more/changePwd",
        name: "修改登录密码",
        component: () => import("@/views/more/changePwd"),
        meta: {
          openName: "/changePwd",
        },
      },
      {
        path: "/more/changePhone",
        name: "更换手机号码",
        component: () => import("@/views/more/changePhone"),
        meta: {
          openName: "/changePhone",
        },
      },
      {
        path: "/more/tixzhanghu",
        name: "提现账户",
        component: () => import("@/views/more/tixzhanghu"),
        meta: {
          openName: "/tixzhanghu",
        },
      },
      {
        path: "/more/bindZhanghu",
        name: "绑定提现账户",
        component: () => import("@/views/more/bindZhanghu"),
        meta: {
          openName: "/bindZhanghu",
        },
      },
      {
        path: "/more/wechat",
        name: "系统客服",
        component: () => import("@/views/more/wechat"),
        meta: {
          openName: "/wechat",
        },
      },

      // 消息中心
      {
        path: "/msg",
        name: "消息中心",
        component: () => import("@/views/msg"),
        meta: {
          openName: "/msg",
        },
      },
      {
        path: "/msg/detail",
        name: "消息中心-详情",
        component: () => import("@/views/msg/detail"),
        meta: {
          openName: "/msg",
        },
      },
      {
        path: "/msg/richText",
        name: "消息中心-富文本",
        component: () => import("@/views/msg/richText"),
        meta: {
          openName: "/msg",
        },
      },
    ]
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

const wtiterPath = ["/forget", "/register", "/"];
let login = false

export const getLoginUser = (userInfo) => {
  login = true
  return new Promise((resolve, reject) => {
    if (userInfo && userInfo.token) {
      resolve(true)
    } else {
      resolve(false)
    }
    return
    JsBridge.callHandler('getLoginUser', {}, (response) => {
      let res = JSON.parse(response)
      if (res.userInfo) {
        let data = JSON.parse(res.userInfo)
        setStore('USER_INFO', data)
        resolve(true)
      } else {
        resolve(false)
      }
    })
  })
}

export const setStatusBarTextColor = (color) => {
  JsBridge.callHandler('setStatusBarTextColor', color, (res) => {
    console.log('setStatusBarTextColor 响应值 ： ', res);
  })
}

router.beforeEach((to, from, next) => {
  let userInfo = getStore('USER_INFO')

  // 分享不走基座
  if (to.path == "/share") {
    next()
    return
  }

  // 设置头的字体颜色
  if (to.path == '/') {
    setStatusBarTextColor({ color: 'dark' })
  } else {
    setStatusBarTextColor({ color: 'light' })
  }

  //路由导航守卫
  globalLoading(true);

  if (JsBridge.isDev()) {
    next()
    return
  }

  // 第一次加载app，判断是否免登录
  if (!login) {
    getLoginUser().then(res => {
      if (res) {
        next({ path: "/home" });
      } else {
        next();
      }
    })
  } else {
    if (!userInfo && !wtiterPath.includes(to.path)) {
      next({ path: "/" });
      return;
    }
    next();
  }
})

router.afterEach((to, from) => {
  globalLoading(false);
});

export default router;